/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.dialog;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Window;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.ImageIcon;

import org.jxSwing.component.panel.ImagePanel;
import org.jxSwing.component.panel.ImageScrollPanel;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.dialog
 * @date 07/02/2014 - 23:28:32
 */
public class ImageDialog extends Dialog {
	
	private static final long serialVersionUID = 2564815414635964914L;
	
	private final ImageIcon imageIcon;
	
	private ImageScrollPanel imageScrollPanel;
	
	private boolean justifySize = false;
	
	/**
	 * @param title
	 * @param imageIcon
	 */
	public ImageDialog(final String title, final ImageIcon imageIcon) {
	
		this(title, imageIcon, 500, 500);
	}
	
	/**
	 * @param title
	 * @param imageIcon
	 * @param justifySize
	 */
	public ImageDialog(final String title, final ImageIcon imageIcon, final boolean justifySize) {
	
		this(null, title, imageIcon, 500, 500, justifySize);
	}
	
	/**
	 * @param owner
	 * @param title
	 * @param imageIcon
	 * @param justifySize
	 */
	public ImageDialog(final Window owner, final String title, final ImageIcon imageIcon, final boolean justifySize) {
	
		this(owner, title, imageIcon, 500, 500, justifySize);
	}
	
	/**
	 * @param title
	 * @param imageIcon
	 * @param width
	 * @param height
	 */
	public ImageDialog(final String title, final ImageIcon imageIcon, final int width, final int height) {
	
		this(null, title, imageIcon, width, height, false);
	}
	
	/**
	 * @param owner
	 * @param title
	 * @param imageIcon
	 * @param width
	 * @param height
	 * @param justifySize
	 */
	public ImageDialog(final Window owner, final String title, final ImageIcon imageIcon, final int width, final int height, final boolean justifySize) {
	
		super(owner);
		
		this.imageIcon = imageIcon;
		this.justifySize = justifySize;
		
		add(getImageScrollPanel(), BorderLayout.CENTER);
		
		setTitle(title);
		setSize(width, height);
		centralize();
	}
	
	/**
	 * @return imagePanel
	 */
	public final ImagePanel getImagePanel() {
	
		return getImageScrollPanel().getImagePanel();
	}
	
	/**
	 * @return the imageScrollPanel
	 */
	public final ImageScrollPanel getImageScrollPanel() {
	
		if(imageScrollPanel == null) {
			
			imageScrollPanel = new ImageScrollPanel(imageIcon);
			imageScrollPanel.addComponentListener(new ComponentAdapter() {
				
				@Override
				public void componentResized(final ComponentEvent componentEvent) {
				
					justifySize();
				}
			});
		}
		
		return imageScrollPanel;
	}
	
	/**
	 * 
	 */
	public final void justifySize() {
	
		//
		setCursor(new Cursor(Cursor.WAIT_CURSOR));
		
		//
		if(justifySize) {
			
			final ImageIcon image = getImagePanel().getImage();
			
			getImagePanel().setImage(image, (double)getWidth() / image.getIconWidth());
		}
		
		//
		setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
	}
	
	/**
	 * @param imageIcon
	 * @param scale
	 */
	public final void setImageIcon(final ImageIcon imageIcon, final double scale) {
	
		getImagePanel().setImage(imageIcon, scale);
		
		justifySize();
	}
}
